<?php
/**
 * 群组禁言
 * @author hejinyu
 */

namespace RongCloud\Lib\Group\Gag;

use RongCloud\Lib\Request;
use RongCloud\Lib\Utils;

class Gag
{
    /**
     * 群组模块路径
     *
     * @var string
     */
    private $jsonPath = 'Lib/Group/Gag/';

    /**
     * 请求配置文件
     *
     * @var string
     */
    private $conf = "";

    /**
     * 校验配置文件
     *
     * @var string
     */
    private $verify = "";

    /**
     * Conversation constructor.
     */
    function __construct()
    {
        $this->conf = Utils::getJson($this->jsonPath . 'api.json');
        $this->verify = Utils::getJson($this->jsonPath . '../verify.json');
    }

    /**
     * 添加群组禁言
     *
     * @param $Group array 添加群组禁言 参数
     * @param
     * $Group = [
     * 'id'=> 'ujadk90ha',//群组 id
     * 'members'=>[ //禁言人员列表
     * ['id'=> 'ujadk90ha']
     * ]
     * ,
     * 'minute'=>50  //    禁言时长
     * ];
     * @return array
     */
    public function add(array $Group = [])
    {
        $conf = $this->conf['add'];
        $verify = $this->verify['group'];
        $verify = ['id' => $verify['id'], 'members' => $verify['members']];
        $verify = array_merge($verify, ['minute' => $this->verify['minute']]);
        $error = (new Utils())->check([
            'api'    => $conf,
            'model'  => 'group',
            'data'   => $Group,
            'verify' => $verify,
        ]);
        if ($error) return $error;
        foreach ($Group['members'] as &$v) {
            $v = $v['id'];
        }
        $Group = (new Utils())->rename($Group, [
            'id'      => 'groupId',
            'members' => 'userId',
        ]);
        $result = (new Request())->Request($conf['url'], $Group);
        $result = (new Utils())->responseError($result, $conf['response']['fail']);

        return $result;
    }

    /**
     * 解除禁言
     *
     * @param $Group array 解除禁言 参数
     * @param
     * $Group = [
     * 'id'=> 'ujadk90ha',//群组 id
     * 'members'=>[ //解除禁言人员列表
     * ['id'=> 'ujadk90ha']
     * ]
     * ];
     * @return array
     */
    public function remove(array $Group = [])
    {
        $conf = $this->conf['remove'];
        $verify = $this->verify['group'];
        $verify = ['id' => $verify['id'], 'members' => $verify['members']];
        $error = (new Utils())->check([
            'api'    => $conf,
            'model'  => 'group',
            'data'   => $Group,
            'verify' => $verify,
        ]);
        if ($error) return $error;
        foreach ($Group['members'] as &$v) {
            $v = $v['id'];
        }
        $Group = (new Utils())->rename($Group, [
            'id'      => 'groupId',
            'members' => 'userId',
        ]);
        $result = (new Request())->Request($conf['url'], $Group);
        $result = (new Utils())->responseError($result, $conf['response']['fail']);

        return $result;
    }

    /**
     * 查询禁言成员列表
     *
     * @param $Group array 解除禁言 参数
     * @param
     * $Group = [
     * 'id'=> 'ujadk90ha',//群组 id
     * ];
     * @return array
     */
    public function getList(array $Group = [])
    {
        $conf = $this->conf['getList'];
        $verify = $this->verify['group'];
        $verify = ['id' => $verify['id']];
        $error = (new Utils())->check([
            'api'    => $conf,
            'model'  => 'group',
            'data'   => $Group,
            'verify' => $verify,
        ]);
        if ($error) return $error;
        $Group = (new Utils())->rename($Group, [
            'id' => 'groupId',
        ]);
        $result = (new Request())->Request($conf['url'], $Group);
        $result = (new Utils())->responseError($result, $conf['response']['fail']);
        if ($result['code'] == 200) {
            $result = (new Utils())->rename($result, ['users' => 'members']);
            foreach ($result['members'] ?: [] as $k => &$v) {
                $v = (new Utils())->rename($v, ['userId' => 'id']);
            }
        }

        return $result;
    }

    /**
     * 群组整体禁言
     *
     * @date   2019-10-23
     * @author edmmss
     * @param array $group
     * $group = [
     *      'id'=> 'ujadk90ha',// 群组 id
     * ];
     * @return array|int|mixed|null
     */
    public function overallAdd(array $group = [])
    {
        $conf = $this->conf['overallAdd'];
        $verify = $this->verify['group'];
        $verify = ['id' => $verify['id']];
        $error = (new Utils())->check([
            'api'    => $conf,
            'model'  => 'group',
            'data'   => $group,
            'verify' => $verify,
        ]);
        if ($error) return $error;

        $group = (new Utils())->rename($group, [
            'id' => 'groupId',
        ]);
        $result = (new Request())->Request($conf['url'], $group);
        $result = (new Utils())->responseError($result, $conf['response']['fail']);

        return $result;
    }

    /**
     * 添加禁言用户白名单
     *
     * @date   2019-10-23
     * @author edmmss
     * @param array $group
     *  $group = [
     *      'id'=> 'ujadk90ha',// 群组 id
     *      'members'=> [ // 解除禁言人员列表
     *          ['id'=> 'ujadk90ha']
     *      ]
     * ];
     * @return array|int|mixed|null
     */
    public function whitelistAdd(array $group = [])
    {
        $conf = $this->conf['whitelistAdd'];
        $verify = $this->verify['group'];
        $verify = ['id' => $verify['id'], 'members' => $verify['members']];
        $error = (new Utils())->check([
            'api'    => $conf,
            'model'  => 'group',
            'data'   => $group,
            'verify' => $verify,
        ]);
        if ($error) return $error;
        foreach ($group['members'] as &$v) {
            $v = $v['id'];
        }
        $group = (new Utils())->rename($group, [
            'id'      => 'groupId',
            'members' => 'userId',
        ]);
        $result = (new Request())->Request($conf['url'], $group);
        $result = (new Utils())->responseError($result, $conf['response']['fail']);

        return $result;
    }

    /**
     * 群组整体移除禁言
     *
     * @date   2019-10-23
     * @author edmmss
     * @param array $group
     * $group = [
     *      'id'=> 'ujadk90ha',// 群组 id
     * ];
     * @return array|int|mixed|null
     */
    public function rollbackOverallGroup(array $group = [])
    {
        $conf = $this->conf['rollbackOverallGroup'];
        $verify = $this->verify['group'];
        $verify = ['id' => $verify['id']];
        $error = (new Utils())->check([
            'api'    => $conf,
            'model'  => 'group',
            'data'   => $group,
            'verify' => $verify,
        ]);
        if ($error) return $error;

        $group = (new Utils())->rename($group, [
            'id' => 'groupId',
        ]);
        $result = (new Request())->Request($conf['url'], $group);
        $result = (new Utils())->responseError($result, $conf['response']['fail']);

        return $result;
    }

    /**
     * 移除禁言用户白名单
     *
     * @date   2019-10-23
     * @author edmmss
     * @param array $group
     * $group = [
     *      'id'=> 'ujadk90ha',// 群组 id
     *      'members'=> [ // 解除禁言人员列表
     *          ['id'=> 'ujadk90ha']
     *      ]
     * ];
     * @return array|int|mixed|null
     */
    public function rollbackWhitelist(array $group = [])
    {
        $conf = $this->conf['rollbackWhitelist'];
        $verify = $this->verify['group'];
        $verify = ['id' => $verify['id'], 'members' => $verify['members']];
        $error = (new Utils())->check([
            'api'    => $conf,
            'model'  => 'group',
            'data'   => $group,
            'verify' => $verify,
        ]);
        if ($error) return $error;
        foreach ($group['members'] as &$v) {
            $v = $v['id'];
        }
        $group = (new Utils())->rename($group, [
            'id'      => 'groupId',
            'members' => 'userId',
        ]);
        $result = (new Request())->Request($conf['url'], $group);
        $result = (new Utils())->responseError($result, $conf['response']['fail']);

        return $result;
    }
}